﻿using log4net;
using log4net.Appender;
using log4net.Core;
using log4net.Layout;
using log4net.Repository.Hierarchy;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;

namespace CommonUtils
{
    /// <summary>
    /// a wrapper for log4net
    /// </summary>
    public class Log4netWrapper
    {
        public static int MaxLogFiles = 10;
        const string DatePattern = "_yyyyMMddHHmm";
        const string MaxLogSize = "200MB";

        ILog _log = null;

        public Log4netWrapper(string moduleName)
        {
            string strToday = DateTime.Now.ToString("yyyy-MM-dd");
            if (!string.IsNullOrEmpty(moduleName))
            {
                moduleName = $"{strToday}.{moduleName}";
            }
            else
            {
                moduleName = strToday;
            }
            _log = create(moduleName);
        }



        public void Info(string content)
        {
            if (_log != null)
                _log.Info(content);
        }



        public static Log4netWrapper Create(string moduleName = "")
        {
            if (string.IsNullOrEmpty(moduleName))
            {
                moduleName = DateTime.Now.ToString("yyyy-MM-dd");
            }
            return new Log4netWrapper(moduleName);
        }



        static Log4netWrapper _default;
        public static Log4netWrapper Default
        {
            get
            {
                if (_default == null || _default._log == null)
                    _default = Log4netWrapper.Create();
                return _default;
            }
        }


        #region private methods
        ILog create(string loggerName)
        {
            try
            {
                if (string.IsNullOrEmpty(LogConfigure.LogFolder) || !Directory.Exists(LogConfigure.LogFolder))
                    return null;
                if (!Directory.Exists(LogConfigure.LogFolder))
                    return null;

                string fileName = Path.Combine(LogConfigure.LogFolder, loggerName + ".log");
                var hierarchy = (Hierarchy)LogManager.GetRepository();
                hierarchy.Threshold = Level.Debug;
                var loggerA = hierarchy.LoggerFactory.CreateLogger(hierarchy, "LoggerA");
                loggerA.Hierarchy = hierarchy;
                loggerA.AddAppender(CreateFileAppender(loggerName, fileName));
                loggerA.Repository.Configured = true;
                loggerA.Level = Level.Debug;

                ILog logA = new LogImpl(loggerA);
                return logA;
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex);
            }
            return null;
        }

        static IAppender CreateFileAppender(string name, string fileName)
        {
            PatternLayout patternLayout = new PatternLayout();
            patternLayout.ConversionPattern = "%message%newline";
            patternLayout.ActivateOptions();

            RollingFileAppender appender = new RollingFileAppender();
            appender.Name = name;
            appender.File = fileName;
            appender.AppendToFile = true;
            appender.MaxSizeRollBackups = MaxLogFiles;
            appender.RollingStyle = RollingFileAppender.RollingMode.Size;
            appender.MaximumFileSize = "200MB";
            appender.Layout = patternLayout;
            appender.LockingModel = new FileAppender.MinimalLock();
            appender.StaticLogFileName = false;
            appender.ActivateOptions();
            return appender;
        }
        #endregion

    }
}
